package com.android.inger;


//import android.R.color;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import android.text.util.Linkify;
import java.util.regex.Pattern;

public class LiftCheckerBeta extends Activity {
	public static final int ZEILE = 50;
//Quelle: Original
//	http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-using-domparser
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
//		LayoutInflater inflater = getLayoutInflater();
//		View layout = inflater.inflate(R.layout.toast_layout,
//		(ViewGroup) findViewById(R.id.toast_layout_root));
// 
//		TextView text = (TextView) layout.findViewById(R.id.text);
//		text.setText("Daten werden von mvg-zoom.de geholt!");
//
//		Toast toast = new Toast(getApplicationContext());
//		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
//		toast.setDuration(Toast.LENGTH_LONG);
//		toast.setView(layout);
//		toast.show();
		
		ProgressDialog dialog = ProgressDialog.show(LiftCheckerBeta.this, "", 
                        "Loading. Please wait...", false,true);
		
		
		String baseurl="http://www.mvg-zoom.de/fahrgastinfo/do?_flowId=showStation&id=";
		String station[][]=new String[5][2];
		station[3]=new String[]{"OB","Ostbahnhof"};
		station[2]=new String[]{"OD","Odeonsplatz"};
		station[4]=new String[]{"MP","Marienplatz"};
		station[1]=new String[]{"UN","Universität"};
		station[0]=new String[]{"KA","Stachus"};
		
		super.onCreate(savedInstanceState);
//		/** Create a new layout to display the view */
//		setContentView(R.layout.main);
//		LinearLayout layout = new LinearLayout(this);
//		layout.setOrientation(1);
//		layout.setVerticalFadingEdgeEnabled(true);
//		
//		layout.setBackgroundColor(getResources().getColor(R.color.white));
//		
		/** Create a new textview array to display the results */
		TextView out= new TextView(this);
		TextView deviceStatus = new TextView(this);
		TextView stationName= new TextView(this);
		
		setContentView(R.layout.main); 
		LinearLayout ll = (LinearLayout) findViewById(R.id.ll); 
		ll.setBackgroundColor(getResources().getColor(R.color.white));
		String lageDescription=new String();
		String type=new String();
		String description=new String();
		String geplanterAusfall=new String();
		int lageStartIndex;
		int lageEndIndex;
		int defekt;
		
		try {
					StatusReport report=new StatusReport();
		report.create();
		//report.checkWhichIsDefekt();
		deviceStatus = new TextView(this);
		deviceStatus.setLinksClickable(true);
		deviceStatus.setLinkTextColor(R.color.red);
		deviceStatus.setTextColor(getResources().getColor(R.color.blue));
		deviceStatus.setText(report.out +
				"x\ndefekte http://www.google.de MP "+report.badLifts+
				"x\ndefekte rolltreppen "+report.badStairs+
				"x\nreportArrayCounter "+report.reportArrayCounter+
				"x\nstatus "+report.defekt+
				"x\nInhalt "+report.statusReport[ZEILE][0]+
				"x\nInhalt "+report.statusReport[ZEILE][1]+
				"x\nInhalt "+report.statusReport[ZEILE][2]+
				"x\nInhalt "+report.statusReport[ZEILE][3]+
				"x\nInhalt "+report.statusReport[ZEILE][4]
				);
	    // Recognize phone numbers and web URLs
    Linkify.addLinks(deviceStatus, Linkify.PHONE_NUMBERS | Linkify.WEB_URLS);

    // Recognize all of the default link text patterns 
    Linkify.addLinks(deviceStatus, Linkify.ALL);

    // Disable all default link detection
    Linkify.addLinks(deviceStatus, 0);
	Pattern wikiWordMatcher = Pattern.compile("\\b[A-Z]+([A-Z]+)\\b");
String scheme ="http://www.mvg-zoom.de/fahrgastinfo/do?_flowId=showStation&id=";
Linkify.addLinks(deviceStatus, wikiWordMatcher, scheme);
		ll.addView(deviceStatus);
			for(int s=0;s <station.length;s++){
			stationName= new TextView(this);
			stationName.setText(""+station[s][1]);
			stationName.setTextSize(25);
			ll.addView(stationName);
			URL url = new URL(baseurl+station[s][0]);
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(new InputSource(url.openStream()));
			doc.getDocumentElement().normalize();

			
			NodeList nodeList=doc.getElementsByTagName("div");
			defekt=0;
			

			for (int i = 0; i < nodeList.getLength(); i++) {
				
				
				Element itemList=(Element)nodeList.item(i);
			
				if(itemList.getAttribute("class").equalsIgnoreCase("infobox_inBetrieb")||itemList.getAttribute("class").equalsIgnoreCase("infobox_ausserBetrieb"))
					{
						type =itemList.getElementsByTagName("b").item(0).getTextContent();
						description =itemList.getTextContent(); 
						lageStartIndex = description.indexOf("Lage");  
						lageEndIndex = description.indexOf("Status");
						
						lageDescription=description.substring(lageStartIndex, lageEndIndex);
							
						if(itemList.getAttribute("class").equalsIgnoreCase("infobox_ausserBetrieb"))

						{	//Defekte Elemente rot ausgeben
							deviceStatus = new TextView(this);
							//deviceStatus.setTextSize(getResources().getColor(R.layout.main));
							deviceStatus.setTextColor(getResources().getColor(R.color.red));
							deviceStatus.setText(type + "\n"+ lageDescription+"\n" );
							ll.addView(deviceStatus);
							defekt++;
							

						} 
								
						else if(itemList.getAttribute("class").equalsIgnoreCase("infobox_inBetrieb"))
							//finde geplanten ausfall	
						{ if(itemList.getAttribute("class").equalsIgnoreCase("infobox_ausserBetrieb_inlineInBetrieb")){
						
							geplanterAusfall =itemList.getTextContent();
							//geplanterAusfall =itemList.getElementsByTagName("div").item(0).getTextContent();

								//	getTextContent();
						//intakte Elemente gruen ausgeben
							deviceStatus = new TextView(this);
							deviceStatus.setTextColor(getResources().getColor(R.color.black));
							deviceStatus.setText("xxx"+geplanterAusfall);
							ll.addView(deviceStatus);
						}
						}
				}

			}
			if (defekt==0){	
				
				deviceStatus = new TextView(this);
				//deviceStatus.setTextSize(R.layout.main);
				deviceStatus.setTextColor(getResources().getColor(R.color.green));
				deviceStatus.setText("alle Lifte und Rolltreppen intakt"+"\n");
				ll.addView(deviceStatus);
			}
			
		}
			dialog.cancel();

				
		} catch (Exception e) {
			
			System.out.println("XML Pasing Excpetion = " + e);
			
			out.setTextColor(getResources().getColor(R.color.red));
			out.setText("Es ist ein Fehler aufgetreten:   " + e);
			ll.addView(out);
			
		}
		
		

	}
}
